Skip to content

[perf] fix: guard total_tokens behind is_hybrid_model to restore pure-transformer perf - #3585

Merged
yaoyu-33 merged 1 commit into
mainfrom
chcui/total-tokens-hybrid-guard
Apr 30, 2026
Merged

[perf] fix: guard total_tokens behind is_hybrid_model to restore pure-transformer perf#3585
yaoyu-33 merged 1 commit into
mainfrom
chcui/total-tokens-hybrid-guard

Conversation

@cuichenx

Copy link
Copy Markdown
Contributor

PR #3484 added `total_tokens` unconditionally to `PackedSeqParams` in `gpt_step`, `llava_step`, and `vlm_step`. This triggers
`PackedSeqParams.post_init` (introduced by MCore PR #3373) on every forward step for all models — `torch.cat`, `repeat_interleave`,
`arange` — for all models.

`seq_idx` is only consumed by Mamba/SSM layers. For pure-transformer models this work is wasted, causing a ~10% throughput regression on
GB300 70B LoRA.

Fix

Gate `total_tokens` on `TransformerConfig.is_hybrid_model` in `gpt_step`, `llava_step`, and `vlm_step`. `config` is already available
via `get_model_config(model)` in each function.

  • Pure-transformer: `is_hybrid_model=False` → `total_tokens=None` → `post_init` is a no-op
  • Mamba/hybrid: `is_hybrid_model=True` → `total_tokens` set as before → `seq_idx` built correctly

Test plan

  • Verify GB300 70B LoRA throughput regression is resolved
  • Run hybrid/Mamba model to confirm `seq_idx` still generated correctly
  • CI: `tests/unit_tests/training/utils/test_packed_seq_utils.py`

…-transformer perf

PackedSeqParams.__post_init__ (added by MCore PR #3373) runs expensive CUDA ops
(torch.repeat_interleave, arange, cat) to build seq_idx every forward step when
total_tokens is set.  seq_idx is only consumed by Mamba/SSM layers, so passing
total_tokens unconditionally for all models caused ~10% throughput regression on
pure-transformer configs (e.g. GB300 70B LoRA).

Gate total_tokens on config.is_hybrid_model in gpt_step, llava_step, and vlm_step.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Chen Cui <chcui@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Apr 29, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@cuichenx
cuichenx requested a review from yaoyu-33 April 29, 2026 21:34
@cuichenx

Copy link
Copy Markdown
Contributor Author

/ok to test b3888ff

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The gating logic is correct and consistent across all three step files. getattr with a False default is a safe fallback, and the existing test_packed_seq_utils.py tests already cover both the total_tokens-present and absent paths at the utility layer.

@yaoyu-33
yaoyu-33 merged commit e699b95 into main Apr 30, 2026
86 of 87 checks passed
@yaoyu-33
yaoyu-33 deleted the chcui/total-tokens-hybrid-guard branch April 30, 2026 01:41
yaoyu-33 added a commit to DAISY-gh/Megatron-Bridge that referenced this pull request Apr 30, 2026
Resolved vlm_step.py conflict: kept THD packing logic and adopted
upstream's perf optimization that gates total_tokens behind
config.is_hybrid_model (NVIDIA-NeMo#3585).

Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
vasunvidia pushed a commit to vasunvidia/Megatron-Bridge that referenced this pull request Jun 10, 2026
…-transformer perf (NVIDIA-NeMo#3585)

Signed-off-by: Chen Cui <chcui@nvidia.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Vasudevan Rengasamy <vrengasamy@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants